package org.apache.solr.formula;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Query;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.handler.component.ResponseBuilder;
import org.apache.solr.handler.component.SearchComponent;
import org.apache.solr.search.DocListAndSet;
import org.apache.solr.search.SolrIndexSearcher;
import org.apache.solr.search.SolrQueryParser;

import cn.ac.cintcm.prespring.cluster.CalSimilarity;
import cn.ac.cintcm.prespring.cluster.Similarity;
import cn.ac.cintcm.prespring.util.IndexFile;
import cn.ac.cintcm.prespring.util.StringUtil;

public class SimilarComponent  extends SearchComponent {

    private static final int MAX_RETURN_NUM = 100;
    private float defaultSimilarity = 2/3f;

    @Override
    public String getDescription() {
        
        return "方剂之间的相似性，使用方剂的属性的向量间的相似度来构建";
    }

    @Override
    public String getSource() {
        
        return "prespring1.0";
    }

    @Override
    public String getSourceId() {
        return "SimilarComponent.java";
    }

    @Override
    public String getVersion() {
        return "1.0";
    }

    @Override
    public void prepare(ResponseBuilder rb) throws IOException {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void process(ResponseBuilder rb) throws IOException {
        SolrParams p = rb.req.getParams();
        SolrIndexSearcher searcher = rb.req.getSearcher();

        List<String> values = new ArrayList<String>();
        values.add("cids");
        ElementsIndex ei = new ElementsIndex(searcher, "id", values);
        
        CalSimilarity cs = new CalSimilarity(
                new IndexFile(ei.getIndex("cids")));
        String idstr = p.get("q");
        String[] ida = idstr.split(":");
        if (ida != null && ida.length == 2) {
            Similarity sm = cs.getSimilarElements(defaultSimilarity , ida[1], null);
            List<String> idStrs = new ArrayList<String>();
            for (Integer id : sm.getSimilarElements()) {
                idStrs.add(StringUtil.getId(id));
            }
            if (idStrs.size() > 0) {
                String strs = StringUtil.array2String(idStrs, " ");
                DocListAndSet results = new DocListAndSet();
                SolrQueryParser parser = rb.req.getSchema().getSolrQueryParser("id");
                parser.setDefaultOperator(QueryParser.Operator.OR);
                Query query = null;
                try {
                    query = parser.parse(strs);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                results.docList = searcher.getDocList(query, null, null, 0, MAX_RETURN_NUM, 0);
                rb.rsp.add( "similar", results.docList );
            }
        }
    }

}
